﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bookshop
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        SqlConnection Con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#\MyProject\BookShopDb.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;");

        private void label9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        public static string UserName = "";
        public static int UserId = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            if (UNameTb.Text == "" || PasswTb.Text == "")
            {
                MessageBox.Show("请输入账号和密码！");
            }
            else
            {
                try
                {
                    string query = "select count(*) from UserTb1 where UName=@UN and UPassword=@PW";
                    Con.Open();
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.Parameters.AddWithValue("@UN", UNameTb.Text);
                    cmd.Parameters.AddWithValue("@PW", PasswTb.Text);
                    int count = Convert.ToInt32(cmd.ExecuteScalar());
                    if (count == 1)
                    {
                        UserName = UNameTb.Text;
                        string query2 = "select UId from UserTb1 where UName=@UN and UPassword=@PW";
                        SqlCommand cmd2 = new SqlCommand(query2, Con);
                        cmd2.Parameters.AddWithValue("@UN", UNameTb.Text);
                        cmd2.Parameters.AddWithValue("@PW", PasswTb.Text);
                        UserId = Convert.ToInt32(cmd2.ExecuteScalar());

                        this.Hide();
                        Billing obj = new Billing();
                        obj.Show();
                    }
                    else
                    {
                        MessageBox.Show("账号或密码错误！");
                    }
                    Con.Close();
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("登录失败！" + Ex.Message);
                }
                finally
                {
                    if(Con.State == ConnectionState.Open)
                    {
                        Con.Close();
                    }
                }
            }
        }

        private void PasswTb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true; // 阻止默认的 Enter 键行为
                button1_Click(sender,e);
            }
        }

        private void label4_MouseClick(object sender, MouseEventArgs e)
        {
            this.Hide();
            AdminLogin obj = new AdminLogin();
            obj.Show();
        }
    }
}
